/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmThongKeDoanhThu.java
 *
 * Created on May 31, 2011, 11:14:40 PM
 */

package Presentation;

import RSCall.ChiTietBaoRSc;
import RSCall.GiaTienQCBangRSc;
import RSCall.GiaTienQCBaoRSc;
import RSCall.GiaTienQCToBuomRSc;
import RSCall.PhieuDangKyRSc;
import RSCall.QCBangRSc;
import RSCall.QCBaoRSc;
import RSCall.ToBuomRSc;
import Entities.GIATIEN_QC_BANG;
import Entities.GIATIEN_QC_BAO;
import Entities.GIATIEN_TOBUOM;
import Entities.PHIEUDANGKY;
import Entities.QC_BANG;
import Entities.QC_BAO;
import Entities.TOBUOM;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author VooKa
 */
public class frmThongKeDoanhThu extends javax.swing.JInternalFrame {

    /** Creates new form frmThongKeDoanhThu */
    public frmThongKeDoanhThu() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cbxThang = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        tbxNam = new javax.swing.JTextField();
        btnDanhSach = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("titlefrmThongKeDoanhThu"));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel1.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("month"));

        cbxThang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel2.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("year"));

        tbxNam.setText("2011");
        tbxNam.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxNamKeyTyped(evt);
            }
        });

        btnDanhSach.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnDanhSach.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("list"));
        btnDanhSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDanhSachActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 963, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(140, 140, 140)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(cbxThang, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(tbxNam, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(btnDanhSach)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbxThang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(tbxNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDanhSach))
                .addGap(35, 35, 35)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.LoadData(cbxThang.getSelectedIndex()+1, Integer.valueOf(tbxNam.getText()));
    }//GEN-LAST:event_formInternalFrameOpened

    private void btnDanhSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDanhSachActionPerformed
        // TODO add your handling code here:
        if(tbxNam.getText().length()!=4)
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("namnhapkhongdung"));
        else
            this.LoadData(cbxThang.getSelectedIndex()+1, Integer.valueOf(tbxNam.getText()));
    }//GEN-LAST:event_btnDanhSachActionPerformed

    private void tbxNamKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxNamKeyTyped
        // TODO add your handling code here:
        Utilities.Utilities.tbxKeyTyped(evt);
    }//GEN-LAST:event_tbxNamKeyTyped
    private void LoadData(int thang, int nam){
        try
        {
            PhieuDangKyRSc _phieuDangKy = new PhieuDangKyRSc();
            QCBangRSc _bang = new QCBangRSc();
            QCBaoRSc _bao = new QCBaoRSc();
            ToBuomRSc _toBuom = new ToBuomRSc();
            ChiTietBaoRSc _chiTiet = new ChiTietBaoRSc();
            
            ArrayList<PHIEUDANGKY> lst_PhieuDK = _phieuDangKy.GetAllRows();
            
            ArrayList<QC_BANG> lst_QCBang = _bang.GetAllRows();
            ArrayList<QC_BAO> lst_QCBao = _bao.GetAllRows();
            ArrayList<TOBUOM> lst_ToBuom = _toBuom.GetAllRows();
            
            DefaultTableModel dtm = new DefaultTableModel();
            dtm.addColumn("STT");
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("mapdk"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("ngaydk"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("ngayhethan"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("doanhthu"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("giaqcbang"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("giaqcbao"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("giatobuom"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("makh"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("propertyid"));
            PHIEUDANGKY phieuDK = null;
            QC_BANG qcBang = null;
            QC_BAO qcBao = null;
            TOBUOM qctoBuom = null;
            GiaTienQCBangRSc _giaTienBang = new GiaTienQCBangRSc();
            GiaTienQCBaoRSc _giaTienBao = new GiaTienQCBaoRSc();
            GiaTienQCToBuomRSc _giaTienToBuom = new GiaTienQCToBuomRSc();
            int flag = 0;
            for(int i=0;i<lst_PhieuDK.size();i++)
            {
                Vector _v = new Vector();
                phieuDK = lst_PhieuDK.get(i);
                if((phieuDK.getTuNgay().getMonth()+1) == thang && (phieuDK.getTuNgay().getYear()+1900) == nam)
                {
                    _v.add(i+1);
                    _v.add(phieuDK.getMaPhieuDangKy());
                    _v.add(phieuDK.getTuNgay());
                    _v.add(phieuDK.getDenNgay());
                    _v.add(phieuDK.getSoTien());
                    
                    for(int j=0;j<lst_QCBang.size();j++)
                    {
                        qcBang = lst_QCBang.get(j);
                        if(phieuDK.getMaPhieuDangKy().equals(qcBang.getMaPhieuDangKy()))
                        {
                            GIATIEN_QC_BANG _g = _giaTienBang.GetRowByID(qcBang.getMaGiaTien());
                            _v.add(_g.getGiaTien());
                            flag++;
                        }
                    }
                    if(flag == 0)
                        _v.add(0);
                    flag = 0;

                    for(int j=0;j<lst_QCBao.size();j++)
                    {
                        qcBao = lst_QCBao.get(j);

                        if(phieuDK.getMaPhieuDangKy().equals(qcBao.getMaPhieuDangKy()))
                        {
                            GIATIEN_QC_BAO _g = _giaTienBao.GetRowByID(qcBao.getMaGiaTien());
                            ArrayList<String> lst4 = _chiTiet.GetMaBaoByMaQCBao(qcBao.getMaQCBao());
                            int n = lst4.size();
                            _v.add(_g.getGiaTien()*n);
                            flag++;
                        }
                    }
                    if(flag == 0)
                        _v.add(0);
                    flag = 0;
                    for(int j=0;j<lst_ToBuom.size();j++)
                    {
                        qctoBuom = lst_ToBuom.get(j);
                        if(phieuDK.getMaPhieuDangKy().equals(qctoBuom.getMaPhieuDangKy()))
                        {
                             GIATIEN_TOBUOM _g = _giaTienToBuom.GetRowByID(qctoBuom.getMaGiaTien());
                            _v.add(_g.getGiaTien());
                            flag++;
                        }
                    }
                    if(flag == 0)
                        _v.add(0);
                    flag = 0;
                    _v.add(phieuDK.getMaKH());
                    _v.add(phieuDK.getMaDiaOc());
                    dtm.addRow(_v);
                }
            }
            this.jTable1.setModel(dtm);
            this.jTable1.getColumnModel().getColumn(0).setPreferredWidth(20);

        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDanhSach;
    private javax.swing.JComboBox cbxThang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tbxNam;
    // End of variables declaration//GEN-END:variables

}
